﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;

namespace SES.CMS
{
    public partial class GioHang : BasePage
    {
        ShopingCart vpp = new ShopingCart();
        private static DataTable tb = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadGioHang();
                load();
            }
        }
        protected void loadGioHang()
        {
            Page.Title = "Giỏ Hàng - KMA";
            if (Session["GioHang"] != null)
            {
                tb = (DataTable)Session["GioHang"];
                DataTable dt = new DataTable();
                dt.Columns.Add("STT");
                dt.Columns.Add("SanPhamID");
                dt.Columns.Add("TenSanPham");
                dt.Columns.Add("HinhAnh");
                dt.Columns.Add("SoLuong");
                dt.Columns.Add("DonGia");
                dt.Columns.Add("GiamGia");
                dt.Columns.Add("ThanhTien");

                int i = 0;
                foreach (DataRow dr in tb.Rows)
                {
                    cmsSanPhamDO obj = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = Int64.Parse(dr[1].ToString()) });
                    i++;
                    DataRow row = dt.NewRow();
                    row[0] = dr[0];
                    row[1] = dr[1];
                    row[3] = obj.HinhAnh;
                    row[4] = dr[2];
                    row[6] = obj.GiamGia;
                    if (Session["lang"].ToString() == "VN")
                    {
                        row[2] = obj.TenSanPham;
                        row[5] = obj.DonGia;
                        float price = obj.DonGia * (100 - obj.GiamGia) / 100;
                        row[7] = price * int.Parse(dr[2].ToString());
                    }
                    else
                    {
                        row[2] = obj.eTenSanPham;
                        float curr = float.Parse(new sysConfigBL().Select(new sysConfigDO { ConfigID = 4 }).ConfigValue) / float.Parse(new sysConfigBL().Select(new sysConfigDO { ConfigID = 4 }).eConfigValue);
                        row[5] = (obj.DonGia) / curr;
                        float price = float.Parse(row[5].ToString()) * (100 - int.Parse(row[6].ToString())) / 100;
                        row[7] = int.Parse(dr[2].ToString()) * price; ;
                    }
                    dt.Rows.Add(row);
                }

                if (dt.Rows.Count > 0)
                {
                    GridGioHang.DataSource = dt;
                    GridGioHang.DataBind();
                    float sum = 0;
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        Label lb = GridGioHang.Rows[j].FindControl("lbDG") as Label;
                        if (Session["lang"].ToString() == "VN")
                        {
                            int s = int.Parse(dt.Rows[j].ItemArray[5].ToString());
                            lb.Text = s.ToString("##,## VNÐ");
                            lb = GridGioHang.Rows[j].FindControl("lbThanhTien") as Label;
                            s = int.Parse(dt.Rows[j].ItemArray[7].ToString());
                            lb.Text = s.ToString("##,## VNÐ");
                        }
                        else
                        {
                            int s = int.Parse(dt.Rows[j].ItemArray[5].ToString());
                            lb.Text = s.ToString("##,## VNÐ");
                            lb = GridGioHang.Rows[j].FindControl("lbThanhTien") as Label;
                            s = int.Parse(dt.Rows[j].ItemArray[7].ToString());
                            lb.Text = s.ToString("##,## VNÐ");
                        }
                        sum += float.Parse(dt.Rows[j].ItemArray[7].ToString());
                    }
                    if (Session["lang"].ToString() == "VN")
                    {
                        lblSum.Text = "Tổng: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## VNÐ") + "</span>";
                        lbThanhTien.Text = "Tổng: <span style='color:green;font-weight:bold;'>" + NumberToStringVN.DocTienBangChu((decimal)sum, " đồng") + "</span";
                    }
                    else
                    {
                        lblSum.Text = "Total: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## USD") + "</span>";
                    }
                }
            }
            else
            {
                if (Session["lang"].ToString() == "VN")
                    lbl.Text = "Chua có sản phẩm nào trong giỏ hàng!!";
                else
                    lbl.Text = "Your Cart is Empty!!";
                lblSum.Text = "";
                lbThanhtoan.Visible = false;
            }
        }
        public void Calc(object sender, EventArgs e)
        {
            TextBox tb1 = sender as TextBox;
            GridViewRow row = (GridViewRow)tb1.NamingContainer;
            tb1 = row.FindControl("txtSL") as TextBox;
            Label DG = (Label)row.FindControl("lbDG");
            Label GG = (Label)row.FindControl("lbDGG");

            decimal sum = 0;

            int sl = int.Parse(tb1.Text);
            int gg = int.Parse(GG.Text.Replace("%", "").Trim());
            decimal dg = 0;

            Label TT = (Label)row.FindControl("lbThanhTien");
            decimal ThanhTien = 0;

            if (Session["lang"].ToString() == "VN")
            {
                dg = Decimal.Parse(DG.Text.Replace(" VNÐ", "").Replace(".", "").Trim());
                ThanhTien = (decimal)sl * (dg - ((dg * gg) / 100));

                TT.Text = (sl * (dg - ((dg * gg) / 100))).ToString("##,## VNÐ");

                for (int i = 0; i < GridGioHang.Rows.Count; i++)
                {
                    Label TT1 = (Label)GridGioHang.Rows[i].FindControl("lbThanhTien");
                    Decimal t = Decimal.Parse(TT1.Text.Replace(" VNÐ", "").Replace(".", "").Trim());
                    sum += t;
                }

                DG.Text = dg.ToString("##,## VNÐ");
                lblSum.Text = "Tổng: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## VNÐ") + "</span>";
                lbThanhTien.Text = "Bằng chữ: <span style='color:green;font-weight:bold;'>" + NumberToStringVN.DocTienBangChu((decimal)sum, " đồng") + "</span";
            }
            else
            {
                dg = Decimal.Parse(DG.Text.Replace(" USD", "").Replace(".", "").Trim());
                ThanhTien = (decimal)sl * (dg - ((dg * gg) / 100));


                TT.Text = (sl * (dg - ((dg * gg) / 100))).ToString("##,## USD");

                for (int i = 0; i < GridGioHang.Rows.Count; i++)
                {
                    Label TT1 = (Label)GridGioHang.Rows[i].FindControl("lbThanhTien");
                    Decimal t = Decimal.Parse(TT1.Text.Replace(" USD", "").Replace(".", "").Trim());
                    sum += t;
                }

                DG.Text = dg.ToString("##,## VNÐ");
                lblSum.Text = "Total: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## USD") + "</span>";
            }
            tb = vpp.upDateSL(tb, Int32.Parse(((Label)row.FindControl("lbSTT")).Text), sl, ThanhTien);
        }
        public void load()
        {
            Page.Title = "Giỏ Hàng - KMA";
            bindatalist(new cmsSanPhamBL().SelectNoiBat(0));
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            rpt.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 6;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rpt.Visible = true;
                rpt.DataSource = objPds;
                rpt.DataBind();
            }
            else
            {
                //lbThongbao.Visible = true;
                //lbThongbao.Text = "Không có sản phẩm!";
            }
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            load();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            load();
        }

        protected void GridGioHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridGioHang.PageIndex = e.NewPageIndex;
            loadGioHang();
        }
        protected void GridGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Int64 id = Int64.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "xoa")
            {
                vpp.delete(tb, id);
                Response.Redirect("/GioHang.aspx");
            }
        }

        protected void GridGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label DonGia = e.Row.FindControl("lbDG") as Label;
                Label ThanhTien = e.Row.FindControl("lbThanhTien") as Label;
                if (Session["lang"].ToString() == "VN")
                {
                    DonGia.Text = string.Format(DonGia.Text, "{0:0,0 VNĐ}");
                    ThanhTien.Text = string.Format(ThanhTien.Text, "{0:0,0 VNĐ}");
                }
                else
                {
                    DonGia.Text = string.Format(DonGia.Text, "{0:0,0 USD}");
                    ThanhTien.Text = string.Format(ThanhTien.Text, "{0:0,0 USD}");
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("style", "background-color: #FFFFFF; color: black;");
                e.Row.Attributes.Add("onmouseover", "style.backgroundColor='#CBECF6'");
                e.Row.Attributes.Add("onmouseout", "style.backgroundColor='#FFFFFF'");
            }
        }
    }
}
